package org.fnzn.web.rest;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.fnzn.bean.Element;
import org.fnzn.bean.vo.User;
import org.fnzn.biz.ElementBiz;
import org.fnzn.core.Result;
import org.fnzn.core.TableResultResponse;
import org.fnzn.util.ResultGenerator;
import org.fnzn.web.rest.base.BaseController;

import tk.mybatis.mapper.entity.Example;

import java.util.List;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("element")
public class ElementController extends BaseController<ElementBiz, Element> {
  /**
   * 查询指定菜单下的功能列表
   * @param limit  每页记录数
   * @param page 当前页码
   * @param name
   * @param menuId 菜单id
   * @return
   */
  @RequestMapping(value = "/page", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<Element> page(@RequestParam(defaultValue = "10") int limit,
      @RequestParam(defaultValue = "1") int page,String name, @RequestParam(defaultValue = "0") int menuId) {
    Example example = new Example(Element.class);
    PageHelper.startPage(page,limit,true);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("menuId", menuId);
    if(StringUtils.isNotBlank(name)){
      criteria.andLike("name", "%" + name + "%");
    }
    List<Element> elements = baseBiz.selectByExample(example);
    return new TableResultResponse<Element>(elements.size(), elements);
  }

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  @ResponseBody
  public Result getAuthorityElement(String menuId,HttpSession session) {
   // int userId = userBiz.getUserByUsername(getCurrentUserName()).getId();
    User user = (User) session.getAttribute("user");
    String userId=user.getUser_id();
    List<Element> elements = baseBiz.getAuthorityElementByUserId(userId + "",menuId);
    return ResultGenerator.genSuccessResult(elements);
  }

  @RequestMapping(value = "/user/menu", method = RequestMethod.GET)
  @ResponseBody
  public Result getAuthorityElement(HttpSession session) {
   //int userId = userBiz.getUserByUsername(getCurrentUserName()).getId();
    User user = (User) session.getAttribute("user");
    String userId=user.getUser_id();
    List<Element> elements = baseBiz.getAuthorityElementByUserId(userId + "");
    return ResultGenerator.genSuccessResult(elements);
  }
}
